package leetcode.middle._previous;

//最大股票盈利数，动态规划
public class MaxProfit {

    public static void main(String[] args) {
        MaxProfit profit = new MaxProfit();

        System.out.println(profit.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
    public int maxProfit(int[] prices) {
        int maxOut = prices[0];
        int minIn = prices[0];
        int max = 0;

        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < minIn) {
                minIn = prices[i];
                maxOut = prices[i];
                continue;
            }

            if (prices[i] > maxOut) {
                maxOut = prices[i];
                max = Math.max(max,maxOut - minIn);
                continue;
            }
        }

        return max;
    }
}
